package com.jxd.ordering.controller;

import com.jxd.ordering.annotation.CheckPermission;
import com.jxd.ordering.exception.CommonEnum;
import com.jxd.ordering.exception.serviceexception.PermissionNotAllowedException;
import com.jxd.ordering.model.Emp;
import com.jxd.ordering.model.ResultBody;
import com.jxd.ordering.service.IEmpService;
import com.jxd.ordering.service.IUserService;
import com.jxd.ordering.util.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EmpController
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/25
 * @Version 1.0
 */
@RestController
public class EmpController {
    @Autowired
    private IEmpService empService;
    @Autowired
    private IUserService userService;

    /**
     * 新增员工信息
     * level: 3
     * @param emp 员工对象
     * @return  是否新增成功
     */
    @RequestMapping("/addEmp")
    @CheckPermission("3")
    public ResultBody addEmp(@RequestBody Emp emp) {
        return empService.addEmp(emp) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 修改员工信息
     * level: 3
     * @param emp 员工对象
     * @return  是否修改成功
     */
    @RequestMapping("/editEmp")
    public ResultBody editEmp(@RequestBody Emp emp, HttpServletRequest request) throws PermissionNotAllowedException {
        //临时授权
        if (emp.getEmpno() != userService.getUsernoByUserid(Integer.parseInt(request.getAttribute("userid").toString()))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return empService.editEmp(emp) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 批量删除员工信息
     * level: 3
     * @param empnos 员工编号列表
     * @return 是否成功删除
     */
    @CheckPermission("3")
    @RequestMapping("/delEmp")
    public ResultBody delEmp(@RequestBody List<Integer> empnos) {
        return empService.delEmp(empnos.stream().mapToInt(Integer::intValue).toArray()) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 分页查询员工信息
     * level: 3
     * @param map 包含page, limit
     * @return 分页查询结果
     */
    @RequestMapping("/getEmpByPage")
    public Map<String,Object> list(@RequestBody Map<String,String> map, HttpServletRequest request) throws PermissionNotAllowedException {
        //临时授权
        int userid = Integer.parseInt(request.getAttribute("userid").toString());
        if (map.get("deptno") == null || "".equals(map.get("deptno")) || Integer.parseInt(map.get("deptno")) != userService.getDeptnoByUserid(userid)) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return empService.getAllEmpList(map);
    }

    /**
     * 按员工编号查询员工信息
     * level: 3
     * @param empno 员工编号
     * @return 员工对象
     */
    @RequestMapping("/getEmpByEmpno")
    public Emp getEmpByEmpno(int empno, HttpServletRequest request) throws PermissionNotAllowedException {
        //临时授权
        if (empno != userService.getUsernoByUserid(Integer.parseInt(request.getAttribute("userid").toString()))) {
            PermissionUtil.PermissionControl(request.getAttribute("permission"), 3);
        }
        return empService.getEmpByEmpno(empno);
    }
}
